package com.tfswx.jcyw.cloudfiletest;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.*;
import com.amazonaws.services.s3.model.Bucket;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;

@SpringBootApplication
public class App {

    public static AmazonS3 getAmazonS3(){
        ClientConfiguration clientConfig = new ClientConfiguration();
        //设置连接的超时时间，单位毫秒
        clientConfig.setConnectionTimeout(30*1000);
        //设置 socket 超时时间，单位毫秒
        clientConfig.setSocketTimeout(30*1000) ;
        clientConfig.setProtocol(Protocol.HTTP); //设置 http
        //设置 V4 签名算法中负载是否参与签名，关于签名部分请参看《OOS 开发者文档》
        S3ClientOptions options = new S3ClientOptions();
        options.setPayloadSigningEnabled(true);
        // 创建 client
        AmazonS3 oosClient = new AmazonS3Client(
                new PropertiesCredentials(TestConfig.OOS_ACCESS_ID,
                        TestConfig.OOS_ACCESS_KEY),clientConfig);
        // 设置 endpoint
        oosClient.setEndpoint(TestConfig.OOS_ENDPOINT);
        //设置选项
        oosClient.setS3ClientOptions(options);
        return oosClient;
    }

    public static void main(String[] args) {

        AmazonS3 amazonS3 = getAmazonS3();
        List<Bucket> buckets = amazonS3.listBuckets();
        System.out.println(buckets);



        // new PropertiesCredentials(ctOosConfig.getAk(), ctOosConfig.getSk())
        // AmazonS3 ossClient = new AmazonS3Client(new PropertiesCredentials(ctOosConfig.getAk(), ctOosConfig.getSk()), clientConfig);

        SpringApplication.run(App.class, args);
    }

}
